vim.o.wrap = false;
vim.o.hidden = true;

-- auto update buffer on file changed externally
vim.o.autoread = true;
vim.o.ignorecase = true;

vim.o.scrolloff = 12;

-- backspace to perious line
vim.o.backspace = "indent,eol,start";

vim.o.lazyredraw = true;
vim.o.mouse = "";

vim.o.autoindent = true;
vim.o.tabstop = 2;
vim.o.softtabstop = 0;
vim.o.shiftwidth = 0;
vim.o.expandtab = false;
vim.o.smarttab = false;

vim.o.smartcase = true;
vim.o.ignorecase = true;

-- Don't have `o` add a comment
vim.opt.formatoptions:remove "o"

-- do not auto equalize windows' size
vim.o.equalalways = false;
vim.o.splitbelow = true;
vim.o.splitright = true;

-- use system clipboard
-- requires xclip on x11 or wl-copy & wl-paste on wayland
vim.o.clipboard = "unnamedplus";

vim.o.diffopt = "vertical,internal,filler,closeoff";

vim.keymap.set("n", "<esc>", ":noh<CR>");
vim.keymap.set("i", "jj", "<esc>");

vim.keymap.set("n", "<leader>x", "<cmd>.lua<CR>");
vim.keymap.set("n", "<leader>X", "<cmd>source %<CR>");

vim.keymap.set("n", "<C-h>", "<C-w>h");
vim.keymap.set("n", "<C-j>", "<C-w>j");
vim.keymap.set("n", "<C-k>", "<C-w>k");
vim.keymap.set("n", "<C-l>", "<C-w>l");

vim.keymap.set("t", "jj", "<C-\\><C-N>");

vim.keymap.set("n", "<leader>qo", ":copen<cr>");
vim.keymap.set("n", "<leader>qc", ":cclose<cr>");
vim.keymap.set("n", "<leader>qn", ":cnext<cr>");
vim.keymap.set("n", "<leader>qp", ":cprev<cr>");
vim.keymap.set("n", "<leader>qN", ":clast<cr>");
vim.keymap.set("n", "<leader>qP", ":cfirst<cr>");

vim.keymap.set("n", "<leader>lo", ":lopen<cr>");
vim.keymap.set("n", "<leader>lc", ":lclose<cr>");
vim.keymap.set("n", "<leader>ln", ":lnext<cr>");
vim.keymap.set("n", "<leader>lp", ":lprev<cr>");
vim.keymap.set("n", "<leader>lN", ":llast<cr>");
vim.keymap.set("n", "<leader>lP", ":lfirst<cr>");

vim.api.nvim_create_autocmd({ "TextYankPost" }, {
	callback = function()
		vim.highlight.on_yank { timeout = 700 }
	end
})

vim.api.nvim_create_autocmd('BufReadPost', {
	desc = 'Open file at the last position it was edited earlier',
	pattern = '*',
	callback = function()
		local mark = vim.api.nvim_buf_get_mark(0, '"')
		if mark[1] > 1 and mark[1] <= vim.api.nvim_buf_line_count(0) then
			vim.api.nvim_win_set_cursor(0, mark)
		end
	end
})

vim.api.nvim_create_autocmd({ "BufEnter", "TermOpen" }, {
	callback = function()
		local showN = vim.bo.buftype == ""
				or vim.bo.filetype == "fugitive"
				or vim.bo.filetype == "oil";
		vim.wo.number = showN;
		vim.wo.relativenumber = showN;
		vim.wo.signcolumn = (showN and "auto") or "no";
	end
})

vim.api.nvim_create_autocmd({ "VimEnter", "WinEnter", "BufWinEnter" }, {
	callback = function()
		vim.wo.cursorline = true;
	end
});

vim.api.nvim_create_autocmd({ "WinLeave" }, {
	callback = function()
		vim.wo.cursorline = false;
	end
});

vim.api.nvim_create_augroup("JSLog", { clear = true });
vim.api.nvim_create_autocmd("FileType", {
	pattern = { "javascript", "typescript" },
	group = "JSLog",
	callback = function()
		vim.keymap.set("n", "<leader>L", function()
			local expression = vim.fn.getreg("\"");
			vim.api.nvim_feedkeys(
				"oconsole.log(\"" .. expression .. ": \", " .. expression .. ");",
				"x",
				true
			);
		end)
	end
});
